package org.ilead.iserve.dao.impl;

import java.util.List;

import org.ilead.iserve.dao.IHelpCategoryDAO;
import org.ilead.iserve.model.HelpCategory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class HelpCategoryDAO extends HibernateDaoSupport implements IHelpCategoryDAO{

	@Override
	public HelpCategory findById(Long id) {
		try {
			HelpCategory instance = (HelpCategory) getHibernateTemplate()
					.get("org.ilead.iserve.model.HelpCategory", id);
			return instance;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@Override
	public List<HelpCategory> findByProperty(String propertyName,
			Object value) {
		return findByProperty(propertyName, value, " order by id asc");
	}
	@SuppressWarnings("unchecked")
	public List<HelpCategory> findByProperty(String propertyName, Object value, String orderBy) {
		try {
			String queryString = "from HelpCategory as model where model."
					+ propertyName + "= ? " + orderBy;
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			throw re;
		}
	}


	@Override
	public List<HelpCategory> findByCategoryPid(Long categoryPid) {
		return findByProperty("categoryPid",categoryPid);
	}

	@Override
	public HelpCategory merge(HelpCategory helpCategory) {
		try {
			HelpCategory result = (HelpCategory) getHibernateTemplate()
					.merge(helpCategory);
			return result;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	public static IHelpCategoryDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (IHelpCategoryDAO) ctx.getBean("HelpCategoryDAO");
	}

	@Override
	public HelpCategory findByCategoryId(Long categoryId) {
		List <HelpCategory> list = findByProperty("categoryId",categoryId);
		if(list.size()>0)
			return list.get(0);
		return null;
	}

}
